class AddResource < ActiveRecord::Migration
  def self.up
    @parsed_file = CSV::Reader.parse(File.open('db/testData/resources.csv','r'))
    @parsed_file.each do |row|
      resource = Resource.new
      
      resource.first_name = row[2]
      resource.last_name = row[1]
      resource.title = row[3]
      resource.post_address_1 = row[4]
      resource.post_address_2 = ""
      # now for the fun bit... basically, get the value from the csv,
      # and look it up in the suburbs table. if it exists, get the id
      # and use it, else, add it to the suburbs table
      suburb  = Suburb.find(:first, :conditions => {:suburb_name => row[5]})
      if suburb.nil?
        #create suburb
        suburb = Suburb.create(:suburb_name => row[5], :city_id => 1)
      end
      resource.post_address_suburb_id = suburb.id
      resource.post_address_post_code = row[7]
      resource.cellphone_number = row[8]
      resource.work_telephone_number = row[9]
      resource.home_telephone_number = row[10]
      resource.fax_number = row[11]
      resource.email_address = row[12]
      resource.gender = row[13]
      
      race = Race.find(:first, :conditions => {:race_description => row[14]})
      if race.nil?
        race = Race.create(:race_description => row[14])
      end
      resource.race_id = race.id
            
      resourcetype = ResourceType.find(:first, :conditions => {:resource_type_name => row[16]})
      if resourcetype.nil?
        resourcetype = ResourceType.create(:resource_type_name => row[16])
      end
      
      resource.resource_type_id = resourcetype.id
      
      resource.save
      
        
    end
  end

  def self.down
    Resource.delete_all
  end
end
